package cn.xinfei.xdecision.data.config;

import cn.xinfei.xdecision.data.thread.TraceThreadPoolExecutor;
import cn.xinfei.xdecision.prometheus.thread.PrometheusThreadPool;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 * @author zdy
 */
@Component
public class TaskAsyncConfig {

    /**
     * 组件中心场景
     */
    @Bean("componentAsyncExecutor")
    public TraceThreadPoolExecutor componentAsyncExecutor() {
        return new TraceThreadPoolExecutor(
                0,
                100,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10000),
                new ThreadFactoryBuilder().setNameFormat("component-executor-thread-%d").build(),
                new ThreadPoolExecutor.CallerRunsPolicy(),
                true
        );
    }

    /**
     * 依赖关系场景
     */
    @Bean("dependencyAsyncExecutor")
    public TraceThreadPoolExecutor dependencyAsyncExecutor() {
        return new TraceThreadPoolExecutor(
                0,
                100,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10000),
                new ThreadFactoryBuilder().setNameFormat("dependency-executor-thread-%d").build(),
                new ThreadPoolExecutor.CallerRunsPolicy(),
                true
        );
    }

}
